package com.kangyi.admin.controller;

import com.kangyi.admin.service.BannerService;
import com.kangyi.dto.BannerDTO;
import com.kangyi.dto.BulkOperationDTO;
import com.kangyi.dto.EditBannerDTO;
import com.kangyi.result.Result;
import com.kangyi.vo.BannerListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@RestController
@RequestMapping("/admin/banner")
@Slf4j
@Api(tags = "超级管理员-首页banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @GetMapping("/list")
    @ApiOperation("查询现有的banner")
//    @Cacheable(cacheNames = "banner_", key = "'adminAll_'")
    public Result<List<BannerListVO>> listBanner(){
        log.info("查询现有的banner");
        try {
            List<BannerListVO> bannerListVOList = bannerService.listBanner();
            return Result.success(bannerListVOList,"查询成功!");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/add")
    @ApiOperation("新增Banner")
    @CacheEvict(cacheNames = "banner_",allEntries = true)
    public Result addBanner(BannerDTO bannerDTO){
        log.info("新增Banner");
        try {
            bannerService.addBanner(bannerDTO);
            return Result.success("新增成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }



    @DeleteMapping("/delete")
    @ApiOperation("批量删除banner")
    @CacheEvict(cacheNames = "banner_",allEntries = true)
    public Result deleteBanner(@RequestParam List<Integer> ids){
        log.info("批量删除banner:{}",ids);
        try {
            bannerService.deleteBanner(ids);
            return Result.success("删除成功！");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @PutMapping("/operation")
    @ApiOperation("批量操作启用/禁用")
    @CacheEvict(cacheNames = "banner_",allEntries = true)
    public Result bulkOperation(@RequestBody BulkOperationDTO bulkOperationDTO){
        log.info("批量操作启用/禁用:{}",bulkOperationDTO);
        try {
            bannerService.bulkOperation(bulkOperationDTO);
            return Result.success("操作成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PutMapping("/edit")
    @ApiOperation("修改banner")
    @CacheEvict(cacheNames = "banner_",allEntries = true)
    public Result editBanner(@RequestBody EditBannerDTO editBannerDTO){
        log.info("修改banner:{}",editBannerDTO);
        try {
            bannerService.editBanner(editBannerDTO);
            return Result.success("修改成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
